/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import beans.SeekerFriend;
import beans.SeekerCustomer;
import beans.SeekerImgTag;
import beans.SeekerUploadImg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Dhruv
 */

//retrieves user information if authenticated
public class ValidateUserAction extends Action {

    //constructor
    public ValidateUserAction(SeekerCustomer model) {
        super(model);
    }

    //identifies the operation name
    public String getName() {
        return "ValidateUser";
    }

    public Object perform(HttpServletRequest req, HttpServletResponse resp) {

        //JSP page to dispatch the request to
        String address = "";
        //retrieve customer from database
        //customer might be existing customer or new customer
        model = SeekerCustomer.getCustomer(req.getParameter("name"));

        //if object doesnt exist, neither does the customer
        if (model == null) {
            address = "Error.jsp";            
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("error", "You don't exist. Please register with SuperSeeker.");
        }
        else if(model.isValidated(req.getParameter("name"), req.getParameter("password")))
        {
            address = "ValidateUser.jsp";
            //get user name
            String myName = model.getName();
            //get user's picture
            String myPicture = model.getPicture();
            //get all friend requests to this user
            ArrayList<SeekerFriend> friendRequests = model.getFriendRequests();
            //get all the tags the friends added AFTER they became mutual friends
            ArrayList<SeekerImgTag> allFriendsTags = model.getAllFriendsTags();
            //get all the images the friends added AFTER they became mutual friends
            ArrayList<SeekerUploadImg> allFriendsImages = model.getAllFriendsImages();

            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("allFriendsImages", allFriendsImages);
            req.setAttribute("myName", myName);
            req.setAttribute("myPicture", myPicture);
            req.setAttribute("friendRequests", friendRequests);
            req.setAttribute("allFriendsTags", allFriendsTags);
        }
        //else, you inputted wrong password
        else
        {
            address = "Error.jsp";            
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("error", "You exist, but you entered an invalid password.");
        }

        //below object receives requests from the client and sends them to JSP address
        RequestDispatcher dispatcher = req.getRequestDispatcher(address);
        try {
            dispatcher.forward(req, resp);
        } catch (ServletException ex) {
            Logger.getLogger(ValidateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ValidateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        //returning the object conforms to the Abstract Action class definition
        //object needed to maintain session between requests
        return model;
    }
}
